package com.jiazhong.example.project.bookstore.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.example.project.bookstore.commons.bean.Book;
import com.jiazhong.example.project.bookstore.commons.config.JwtConfig;
import com.jiazhong.example.project.bookstore.commons.mapper.BookMapper;
import com.jiazhong.example.project.bookstore.commons.util.JsonResult;
import com.jiazhong.example.project.bookstore.commons.util.ResultTool;
import com.jiazhong.example.project.bookstore.front.service.BookService;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Cacheable(cacheNames = "findLower", key = "#number")
    public JsonResult findLower(int number) {
        List<Book> list = bookMapper.findLower(number);
        return ResultTool.success(list);
    }

    @Cacheable(cacheNames = "findLowerAll", key = "#page")
    public JsonResult findLowerAll(int page, String token) {
        IPage<Book> iPage = new Page<>(page, 10);
        int start = (page - 1) * 10;
        List<Book> list = bookMapper.findLowerMore(start);
        //list = checkExistsCart(list, token);
        iPage.setRecords(list);
        iPage.setTotal(bookMapper.findLowerMoreCount());
        return ResultTool.success(iPage);
    }

    @Cacheable(cacheNames = "findFresh", key = "#number")
    public JsonResult findFresh(int number) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit " + number);
        queryWrapper.orderByDesc("create_time");
        return ResultTool.success(list(queryWrapper));
    }

    @Cacheable(cacheNames = "findFreshAll", key = "#page")
    public JsonResult findFreshAll(int page, String token) {
        IPage<Book> ipage = new Page<>(page, 10);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.last(" and create_time>(select DATE_ADD(now(),INTERVAL -3 day)) order by create_time desc");
        List<Book> list = list(ipage, queryWrapper);
        //list = checkExistsCart(list, token);
        ipage.setRecords(list);
        return ResultTool.success(ipage);
    }

    @Cacheable(cacheNames = "findBookByCategoryId", key = "#page+'.'+#categoryId")
    public JsonResult findBookByCategoryId(int page, String categoryId, String token) {
        log.info("调用了BookServiceImpl");
        IPage<Book> iPage = new Page<>(page, 10);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        List<Book> list = list(iPage, queryWrapper);
        //list = checkExistsCart(list, token);
        iPage.setRecords(list);
        return ResultTool.success(iPage);
    }

    @Cacheable(cacheNames = "findBookByBookname", key = "#page+'.'+#bookname")
    public JsonResult findBookByBookname(int page, String bookname, String token) {
        IPage<Book> iPage = new Page<>(page, 10);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", bookname);
        List<Book> list = list(iPage, queryWrapper);
        //list = checkExistsCart(list, token);
        iPage.setRecords(list);
        return ResultTool.success(iPage);
    }

    @Cacheable(cacheNames = "findBookByBooknameAndFresh", key = "#page+'.'+#bookname")
    public JsonResult findBookByBooknameAndFresh(int page, String bookname, String token) {
        IPage<Book> ipage = new Page<>(page, 10);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", bookname);
        queryWrapper.last(" and create_time>(select DATE_ADD(now(),INTERVAL -3 day)) order by create_time desc");
        List<Book> list = list(ipage, queryWrapper);
        //list = checkExistsCart(list, token);
        ipage.setRecords(list);
        return ResultTool.success(ipage);
    }

    @Cacheable(cacheNames = "findBookByBooknameAndLower", key = "#page+'.'+#bookname")
    public JsonResult findBookByBooknameAndLower(int page, String bookname, String token) {
        IPage<Book> iPage = new Page<>(page, 10);
        int start = (page - 1) * 10;
        List<Book> list = bookMapper.findLowerAndBooknameMore(start, "%" + bookname + "%");
        //list = checkExistsCart(list, token);
        iPage.setRecords(list);
        iPage.setTotal(bookMapper.findLowerAndBooknameMoreCount("%" + bookname + "%"));
        return ResultTool.success(iPage);
    }

    @Cacheable(cacheNames = "findBookByBooknameAndCategoryId", key = "#page+'.'+#bookname+'.'+#categoryId")
    public JsonResult findBookByBooknameAndCategoryId(int page, String bookname, String categoryId, String token) {
        IPage<Book> iPage = new Page<>(page, 10);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.like("name", bookname);
        List<Book> list = list(iPage, queryWrapper);
        //list = checkExistsCart(list, token);
        iPage.setRecords(list);
        return ResultTool.success(iPage);
    }
}
